<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexHotBlindRequest;
use App\Http\Requests\StoreHotBlindRequest;
use App\Http\Requests\UpdateHotBlindRequest;
use App\Http\Resources\HotBlindResource;
use App\Models\HotBlind;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class HotBlindController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum')->except('index');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(IndexHotBlindRequest $request)
    {
        $query = HotBlind::query();
        $query->when($request->filled('name'), function ($query) use ($request) {
            $name = $request->input('name');
            return $query->whereHas('blinds',function ($subquery) use ($name) {
                $subquery->where('name', 'like', "%$name%");
            });
        });

        $data = $query->with(['blinds.blindcates', 'blinds.blindtypelabel', 'blinds.blindcates'])->orderBy('order')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response =  HotBlindResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreHotBlindRequest $request, HotBlind $hotBlind)
    {

        // 开启事务
        DB::beginTransaction();
        try {
            // 如果请求中包含 hotBlind 数据
            if ($request->filled('blind_ids')) {
                foreach ($request->input('blind_ids') as $value) {
                    $hotBlind->fill($value);
                    $hotBlind->save();
                }
            }
            // 提交事务
            DB::commit();
            return $this->success('添加热门成功', $hotBlind);
        } catch (\Exception $e) {
            // 发生异常时回滚事务
            DB::rollback();
            return $this->error('添加失败', $e);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(HotBlind $hotBlind, $id)
    {
        $hotBlind = $hotBlind->findOrFail($id);
        return $this->success('查询成功', new HotBlindResource($hotBlind->load(['blinds.blindcates', 'blinds.blindtypelabel'])));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(HotBlind $hotBlind)
    {
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateHotBlindRequest $request, HotBlind $hotBlind, $id)
    {
        $hotBlind = $hotBlind->findOrFail($id);
        $hotBlind->fill($request->input());
        $hotBlind->save();
        return $this->success('更新热门成功', $hotBlind);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(HotBlind $hotBlind, $id)
    {
        if (!isSuperadmin()) {
            return $this->error('暂无权限');
        }
        $hotBlind = $hotBlind->findOrFail($id);
        $hotBlind->delete();
        return $this->success('删除成功', $hotBlind);
    }
}
